<template>
  <el-card class="container">
    <template #header>
      佣金提现
    </template>
    <el-form ref="refForm" :model="state.form"
      label-width="220px"
      autocomplete="off">
      <el-form-item v-for="item in state.form" :key="item.title" :label="item.title">
        <!-- input -->
        <el-input v-if="item.type==='text'" v-model.trim="item.value" :placeholder="item.title" :style="{
            width: '460px',
          }"/>

        <!-- radio -->
        <div v-if="item.type === 'radio'">
          <el-radio-group v-model="item.value">
            <el-radio v-for="option in item.options" :key="option.label" :label="option.value+''">
              {{ option.label }} <span :style="{
                color: 'rgba(0,0,0,.3)'
              }"> - {{ option.tips }}</span>
            </el-radio>
          </el-radio-group>
        </div>

        <!-- checkbox -->
        <div v-if="item.type === 'checkbox'">
          <el-checkbox-group v-model="item.value" style="display: inline">
            <el-checkbox name="checkbox"
              v-for="option in item.options"
              :key="option.value"
              :label="Number(option.value)">{{ option.label }}
            </el-checkbox>
          </el-checkbox-group>
        </div>


        <span class="input-tips" v-if="item.tips"> * {{ item.tips }} </span>
      </el-form-item>
    </el-form>
  </el-card>

  <div class="btn-fixed">
    <el-button type="primary" @click="submit">保 存</el-button>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { AdminConfigGet, AdminConfigPost } from "../../api/server/config";
import { ElMessage } from "element-plus";

// do not use same name with ref
const refForm = ref(null)
const state = reactive({
  form: null
})

onMounted(() => {
  getConfigData()
})

const getConfigData = () => {
  AdminConfigGet({ group: 'withdraw' }).then(({ data: { info } }) => {
    info.forEach(item => (item.type === 'checkbox') && (item.value = item.value.split(',').map(Number)))
    state.form = info
  })
}

const submit = () => {
  let data = JSON.parse(JSON.stringify(state.form))
  data.forEach(item => {
    if (item.type === 'checkbox') {
      item.value = item.value.toString()
    }
  })

  AdminConfigPost(data).then(() => {
    ElMessage.success('配置信息已更新')
  })
}
</script>

<style scoped>
.container {
  box-shadow: none;
}

.container .el-radio-group {
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-start;
}

.container .el-input,
.btn-submit {
  width: 260px !important;
}

.gs-radio-item {
  height: 24px;
  line-height: 24px;
}

.gs-radio-item .el-radio {
  width: 180px;
}

.gs-radio-item .gs-prompt-text {
  font-size: 14px;
  color: #bbb;
}

.gs-radio-item:not(:first-child) {
  margin-top: 8px;
}

.container .el-checkbox {
  font-weight: bold;
}

.btn-fixed {
  position: absolute;
  bottom: 0;
  width: calc(100% - 250px);
  padding: 15px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 10px -3px 10px 0 rgb(0 0 0 / 10%);
}

.btn-fixed .el-button {
  width: 200px;
  height: 46px;
}

.input-tips {
  margin-left: 10px;
  color: rgba(0, 0, 0, .4);
}
</style>
